package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //http://localhost:8080/dish/list?categoryId=1397844263642378242
    @GetMapping("/list")
    public ResultInfo selectByCategoryId(Long categoryId,@RequestParam(defaultValue = "1") Integer status){
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        List<Dish> dishList= dishService.selectByCategoryId(categoryId,status);
        return ResultInfo.success(dishList);
    }
    //POST   /dish/status/1?ids=1397849739276890114,1397850140982161409
    @PostMapping("/status/{status}")
    public ResultInfo updateStatusByid(@PathVariable("status") Integer status,Long[] ids){
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if (ids!=null&&ids.length>0){
            dishService.updateStatusByid(status,ids);
        }
        return ResultInfo.success(null);
    }
    //http://localhost:8080/dish?ids=1524028562602414081,1413385247889891330
    @DeleteMapping("")
    public ResultInfo deleteByIds(Long[] ids){
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if (ids!=null&&ids.length>0){
            dishService.deleteByIds(ids);
        }
       return ResultInfo.success(null);
    }

//http://localhost:8080/dish/1524028562602414081
    @GetMapping("/{id}")
    public ResultInfo findById(@PathVariable("id") long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }
    //http://localhost:8080/dish
    @PostMapping("")
    public ResultInfo save(@RequestBody Dish dish){
        String key="dish_"+dish.getCategoryId();
        redisTemplate.delete(key);
        dishService.saveAndFlavor(dish);
        return ResultInfo.success(null);
    }
    @PutMapping("")
    public ResultInfo update(@RequestBody Dish dish){
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.updateDash(dish);
        return ResultInfo.success(null);
    }

    //http://localhost:8080/dish/page?page=1&pageSize=10&name=发
    @GetMapping("/page")
    public ResultInfo dishPage(@RequestParam("page") Integer pageNo, Integer pageSize, String name) {
        Page<Dish> page = new Page<>(pageNo, pageSize);
        page=dishService.getdishPage(page,name);
        return ResultInfo.success(page);
    }
}
